#include "TcpServer.h"

TcpServer::TcpServer(std::string &ip, uint16_t &port)
{
    acceptor_ = new Acceptor(&loop_, ip, port);
    acceptor_->setnewConnectioncb(std::bind(&TcpServer::newConnection, this, std::placeholders::_1));
    loop_.setTimeCallBack(std::bind(&TcpServer::epolltimeout, this, std::placeholders::_1));
}

void TcpServer::start()
{
    loop_.run();
}
TcpServer::~TcpServer()
{
    delete acceptor_;
    for (auto &aa : conns_)
    {
        delete aa.second;
    }
}

void TcpServer::newConnection(Socket *clientsock)
{
    Connection *conn = new Connection(&loop_, clientsock);

    conn->setCloseCallBack(std::bind(&TcpServer::closeconnection, this, std::placeholders::_1));
    conn->setErrorCallBack(std::bind(&TcpServer::errorconnection, this, std::placeholders::_1));
    conn->setonmessagecb(std::bind(&TcpServer::onmessage, this, std::placeholders::_1, std::placeholders::_2));
    conn->setsendcompletecallback(std::bind(&TcpServer::sendcomplete, this, std::placeholders::_1));
    printf("accept client(fd=%d,ip=%s,port=%d) ok.\n", conn->fd(), conn->getiP().c_str(), conn->getport());
    conns_[conn->fd()] = conn;

    if(newConnectioncb_)
    {
        newConnectioncb_(conn);
    }
    
}

void TcpServer::closeconnection(Connection *conn) // TCP断开连接的回调函数 ，提供channel 回调
{
    if(closeConnectioncb_)
    {
        closeConnectioncb_(conn);
    }
    
    // printf("client(eventfd=%d) disconnected.\n", conn->fd());
    // close(conn->fd()); // 关闭客户端的fd。
    conns_.erase(conn->fd());
    delete conn;
    // closeConnectioncb_(conn);
}
void TcpServer::errorconnection(Connection *conn) // TcP连接错误的回调函数，提供Channel 回调
{

    if(errorConnectioncb_)
    {
        errorConnectioncb_(conn);
    }
    
    // printf("client(eventfd=%d) error.\n", conn->fd());
    // close(conn->fd()); // 关闭客户端的fd。
    conns_.erase(conn->fd());
    delete conn;
}

void TcpServer::onmessage(Connection *conn, std::string message)
{
    if(onMessagecb_)
    {
        onMessagecb_(conn, message);
    }
    
    // message = "reply" + message; // 得到处理后的结果

    // int len = message.size();
    // std::string tempbuf((char *)&len, 4);
    // tempbuf.append(message);
    // // conn->outputbuffer_ = conn->inputbuffer_;
    // // inputbuffer_.clear();
    // // send(conn->fd(), tempbuf.data(), tempbuf.size(), 0);
    // conn->send(tempbuf.data(), tempbuf.size());
}

void TcpServer::sendcomplete(Connection *conn)
{
    // printf("send complete\n");
    if(sendCompletecb_)
    {
        sendCompletecb_(conn);
    }
    
}

void TcpServer::epolltimeout(EventLoop *loop)
{
    // printf("epoll timeout\n");
    if(epollTimeoutcb_)
    {
        epollTimeoutcb_(loop);
    }
    
}

void TcpServer::setnewConnectioncb(std::function<void(Connection *)> fn)
{
    newConnectioncb_ = fn;
}
void TcpServer::setcloseConnectioncb(std::function<void(Connection *)> fn)
{
    closeConnectioncb_ = fn;
}
void TcpServer::seterrorConnectioncb(std::function<void(Connection *)> fn)
{
    errorConnectioncb_ = fn;
}
void TcpServer::setonMessagecb(std::function<void(Connection *, std::string &)> fn)
{
    onMessagecb_ = fn;
}
void TcpServer::setsendCompletecb(std::function<void(Connection *)> fn)
{
    sendCompletecb_ = fn;
}
void TcpServer::setepollTimeoutcb(std::function<void(EventLoop *)> fn)
{
    epollTimeoutcb_ = fn;
}
